import axios, { ResDataType } from "./ajax";

const baseUrl = process.env.REACT_APP_API_URL + "/api/user";

// 获取用户信息
export async function getUserInfoService(): Promise<ResDataType> {
  const url = `${baseUrl}/info`;
  const data = (await axios.get(url)) as ResDataType;
  return data;
}

// 注册
export async function registerService(
  userName: string,
  password: string,
  nickName?: string
): Promise<ResDataType> {
  const url = `${baseUrl}/register`;
  const params = { userName, password, nickName: nickName || userName };
  const data = (await axios.post(url, params)) as ResDataType;
  return data;
}

// 登录
export async function loginService(
  userName: string,
  password: string
): Promise<ResDataType> {
  const url = `${baseUrl}/login`;
  const params = { userName, password };
  const data = (await axios.post(url, params)) as ResDataType;
  return data;
}

// 修改密码
export async function changePasswordService(
  oldPassword: string,
  password: string
): Promise<ResDataType> {
  const url = `${baseUrl}`;
  const params = { oldPassword, password };
  const data = (await axios.patch(url, params)) as ResDataType;
  return data;
}

// 修改昵称
export async function changeNickNameService(
  nickName: string
): Promise<ResDataType> {
  const url = `${baseUrl}/nickName`;
  const params = { nickName };
  const data = (await axios.patch(url, params)) as ResDataType;
  return data;
}
